﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace buzztest1
{
    [Serializable]
    public class GenericFeedItem
    {
        public string Title { get; set; }
        public string Link { get; set; }
        public string Description { get; set; }
        public DateTime PubDate { get; set; }
    }

    public class GenericFeedParser
    {
        public List<GenericFeedItem> ReadFeedItems(string url)
        {
            //create a List of type Dictionary<string,string> for the element names and values
            var items = new List<Dictionary<string, string>>();
            // declare a Dictionary to capture each current Item in the while loop
            Dictionary<string, string> currentItem = null;
            // Wrap a new XmlTextReader around the url of the feed
            var reader = new XmlTextReader(url);
            /// Read each element with the reader
            while (reader.Read())
            {
                // if it's an element, we want to process it
                if (reader.NodeType == XmlNodeType.Element)
                {
                    string name = reader.Name;
                    if (name.ToLowerInvariant() == "item" || name.ToLowerInvariant() == "entry")
                    {
                        // Save previous item
                        if (currentItem != null)
                            items.Add(currentItem);

                        // Create new item
                        currentItem = new Dictionary<string, string>();
                    }
                    else if (currentItem != null)
                    {
                        reader.Read();
                        // some feeds can have duplicate keys, so we don't want to blow up here:
                        // link save its content in href
                        if(name == "link"){
                            try
                            {
                                currentItem.Add(name, reader.HasAttributes.ToString());
                            }
                            catch 
                            {
                                currentItem.Add(name, reader.GetAttribute("href")); 
                            }
                        }
                        else
                        if (!currentItem.Keys.Contains(name))
                            currentItem.Add(name, reader.Value);
                    }
                }
            }

            // now create a List of type GenericFeedItem
            var itemList = new List<GenericFeedItem>();
            // iterate all our items from the reader
            foreach (var d in items)
            {
                var itm = new GenericFeedItem();
                //do a switch on the Key of the Dictionary <string, string> of each item
                foreach (string k in d.Keys)
                {
                    switch (k)
                    {
                        case "title":
                            itm.Title = d[k];
                            break;
                        case "link":
                            itm.Link = d[k];
                            break;
                        case "published":
                        case "pubDate":
                        case "issued":
                            DateTime dt;
                            DateTime.TryParse(d[k], out dt);
                            itm.PubDate = dt != DateTime.MinValue ? dt : DateTime.Now;
                            break;
                        case "content":
                        case "description":
                            itm.Description = d[k];
                            break;
                        default:
                            break;
                    }
                }
                // add the created item to our List
                itemList.Add(itm);
            }
            return itemList;
        }
    }
}
